package DOM;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLUtil {
	// A16TEST003.plmxml
	String filePath = "";
	String locationJT = "";

	public String getLocationJT() {
		return locationJT;
	}

	public void setLocationJT(String locationJT) {
		this.locationJT = locationJT;
	}

	public XMLUtil(String filePath) {
		this.filePath = filePath;
	}

	public static void main(String[] args) {
		try {
			long begin  = System.currentTimeMillis();
			XMLUtil x = new XMLUtil("C:\\a.plmxml");//测试文件a.plmxml在/src/source目录下
		//	read(path);
			x.readXML();
			long end  = System.currentTimeMillis();
			System.out.println("java dom用时"+(end-begin)+"毫秒");
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void getItem(String tempItemID) {
		int index = tempItemID.indexOf("-", 4);// 取得JT单件号第中杠线的位置（例如，J43-6302010/06-行李箱盖护罩）
		String temp = tempItemID.substring(0, index);
		String itemID = temp.substring(0, temp.indexOf("/"));// 获取零件号
		String ietmVesion = temp.substring(temp.indexOf("/") + 1, index);// 获取版本号
		setLocationJT(itemID + "_" + ietmVesion + ".jt");
		//System.out.println("itemID=" + itemID + "    ietmVesion=" + ietmVesion);
	}

	public void readXML() throws ParserConfigurationException, SAXException,
			IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();// 1.获取工厂
		DocumentBuilder builder = factory.newDocumentBuilder();// 2.产生解析器
		Document document = builder.parse(new File(filePath));// 3.解析xml文档，得到代表文档的document
		NodeList list = document.getElementsByTagName("ProductRevisionView");// 4.获取所有ProductRevisionView节点

		for (int i = 0; i < list.getLength(); i++) {// 5.遍历所有所有ProductRevisionView节点
			Node node = list.item(i);
			Element currentElement = null;
			if (node.getNodeType() == Node.ELEMENT_NODE) {// 6.在做节点转换之前先判断，这样安全。
				currentElement = (Element) node;// 7.当前ProductRevisionView节点
				String currentAttributeName = currentElement.getAttribute(
						"name").trim();// 8.当前ProductRevisionView节点的属性名
				if (!currentAttributeName.endsWith("(view)")
						&& currentAttributeName.length() > 0) {// 9.不以(view)结尾的节点，包含jt数据
					getItem(currentAttributeName);
					getAllNode(node);
					// System.out.println("location="
					// + currentRepresentationAttributeName);

				}
			}
		}

		TransformerFactory f = TransformerFactory.newInstance();
		try {
			Transformer tf = f.newTransformer();
			tf.transform(new DOMSource(document), new StreamResult(new File(
					filePath)));
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}

	public void getAllNode(Node node) {
		Element currentElement = null;
		String currentRepresentationAttributeName = "";
		if (node.getNodeType() == Node.ELEMENT_NODE) {
			String currentNodeName = node.getNodeName().trim();
			if (currentNodeName.equals("Representation")) {
				currentElement = (Element) node;// 7.当前ProductRevisionView节点
				currentRepresentationAttributeName = currentElement
						.getAttribute("location").trim();// 8.当前ProductRevisionView节点的属性名
				System.out.println(currentRepresentationAttributeName);
				currentElement.setAttribute("location", getLocationJT());// 设置属性
			}
		}
		NodeList list = node.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			Node child = list.item(i);
			getAllNode(child);
		}
	}

}
